package org.helgoboss.bash

import org.helgoboss.scala_additions.CurrentPlatform
import org.helgoboss.scala_additions.OperatingSystemClass._
import org.helgoboss.osgi_additions._
import org.helgoboss.osgi_additions.metatype._
import org.osgi.service.metatype.{MetaTypeProvider => JMetaTypeProvider}
import org.helgoboss.shell.Shell
import org.osgi.service.cm.ConfigurationAdmin
import java.io.File

class BashActivator extends OsgiContext with OsgiProvider with OsgiConfigurationWatcher with OsgiMetaTypeProvider {
    whenBundleActive {        
        provideMetaType(objectClass)
        
        whenConfigurationUpdated(objectClass.id) { conf =>
            val resultConf = conf match {
                case Some(c) => objectClass.defaultConfig ++ c
                case None => objectClass.defaultConfig
            }
            val exeFile = new File(resultConf("exeFile").asInstanceOf[String])
            val rcFile = resultConf.get("rcFile").map(_.asInstanceOf[String]).map(new File(_))
            new Bash(exeFile = exeFile, rcFile = rcFile).providesService[Shell]
        }
    }
    
    lazy val objectClass = ObjectClass (
        id = "org.helgoboss.bash",
        name = "Bash",
        optionalAttributes = List (
            ElementaryAttribute[String](id = "exeFile", name = "Bash executable path", default = Some(defaultExeFile)),
            ElementaryAttribute[String](id = "rcFile", name = "Bash rc file")
        )
    )
    
    val defaultExeFile = CurrentPlatform.osClass match {
        case Windows => """C:\cygwin\bin\bash.exe"""
        case Unix => "bash"
    }
}
